<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ChannelUtils, getChannelName } from '@/utils/channel';
import { ChannelService } from '@/service/channel';
import { showToast } from '@tarojs/taro';

definePageConfig({
    navigationBarTitleText: '渠道测试'
});

const channelInfo = ref<any>(null);
const testResults = ref<any[]>([]);

onMounted(() => {
    // 获取当前渠道信息
    channelInfo.value = ChannelUtils.getChannelInfo();
    
    // 添加测试结果
    testResults.value.push({
        test: '渠道ID获取',
        result: ChannelUtils.getChannelId() || '无',
        status: ChannelUtils.isFromThirdParty() ? 'success' : 'warning'
    });
    
    testResults.value.push({
        test: '是否第三方渠道',
        result: ChannelUtils.isFromThirdParty() ? '是' : '否',
        status: ChannelUtils.isFromThirdParty() ? 'success' : 'info'
    });
});

async function testChannelAccess() {
    try {
        await ChannelService.reportChannelAccess();
        showToast({
            title: '渠道访问上报成功',
            icon: 'success'
        });
        testResults.value.push({
            test: '渠道访问上报',
            result: '成功',
            status: 'success'
        });
    } catch (error) {
        showToast({
            title: '渠道访问上报失败',
            icon: 'error'
        });
        testResults.value.push({
            test: '渠道访问上报',
            result: '失败',
            status: 'error'
        });
    }
}

async function testChannelValidation() {
    const channelId = ChannelUtils.getChannelId();
    if (!channelId) {
        showToast({
            title: '无渠道ID可验证',
            icon: 'none'
        });
        return;
    }
    
    try {
        const isValid = await ChannelService.validateChannel(channelId);
        showToast({
            title: isValid ? '渠道验证成功' : '渠道验证失败',
            icon: isValid ? 'success' : 'error'
        });
        testResults.value.push({
            test: '渠道验证',
            result: isValid ? '有效' : '无效',
            status: isValid ? 'success' : 'error'
        });
    } catch (error) {
        showToast({
            title: '渠道验证异常',
            icon: 'error'
        });
        testResults.value.push({
            test: '渠道验证',
            result: '异常',
            status: 'error'
        });
    }
}

async function testHospitalList() {
    try {
        const hospitalList = await ChannelService.getHospitalListByChannel(1, 10);
        if (hospitalList) {
            showToast({
                title: `获取到${hospitalList.records?.length || 0}家医院`,
                icon: 'success'
            });
            testResults.value.push({
                test: '医院列表查询',
                result: `成功获取${hospitalList.records?.length || 0}家医院`,
                status: 'success'
            });
            // 医院列表
        } else {
            showToast({
                title: '医院列表查询失败',
                icon: 'error'
            });
            testResults.value.push({
                test: '医院列表查询',
                result: '失败',
                status: 'error'
            });
        }
    } catch (error) {
        showToast({
            title: '医院列表查询异常',
            icon: 'error'
        });
        testResults.value.push({
            test: '医院列表查询',
            result: '异常',
            status: 'error'
        });
    }
}

function clearChannel() {
    ChannelUtils.clearChannelId();
    channelInfo.value = ChannelUtils.getChannelInfo();
    showToast({
        title: '渠道信息已清除',
        icon: 'success'
    });
}
</script>

<template>
    <view class="p-4">
        <view class="text-lg font-bold mb-4">渠道功能测试</view>
        
        <!-- 当前渠道信息 -->
        <view class="bg-gray-100 p-4 rounded-lg mb-4">
            <view class="text-sm font-bold mb-2">当前渠道信息:</view>
            <view class="text-sm">
                <view>渠道ID: {{ channelInfo?.channelId || '无' }}</view>
                <view>是否第三方: {{ channelInfo?.isFromThirdParty ? '是' : '否' }}</view>
                <view>时间戳: {{ channelInfo?.timestamp || '无' }}</view>
            </view>
        </view>
        
        <!-- 测试按钮 -->
        <view class="flex gap-2 mb-4">
            <button class="bg-blue-500 text-white px-4 py-2 rounded" @click="testChannelAccess">
                测试渠道上报
            </button>
            <button class="bg-green-500 text-white px-4 py-2 rounded" @click="testChannelValidation">
                测试渠道验证
            </button>
            <button class="bg-purple-500 text-white px-4 py-2 rounded" @click="testHospitalList">
                测试医院列表
            </button>
            <button class="bg-red-500 text-white px-4 py-2 rounded" @click="clearChannel">
                清除渠道
            </button>
        </view>
        
        <!-- 测试结果 -->
        <view class="bg-white border rounded-lg">
            <view class="p-3 border-b font-bold">测试结果</view>
            <view v-for="(result, index) in testResults" :key="index" 
                  class="p-3 border-b flex justify-between items-center">
                <view class="text-sm">{{ result.test }}</view>
                <view class="text-sm" :class="{
                    'text-green-500': result.status === 'success',
                    'text-red-500': result.status === 'error',
                    'text-yellow-500': result.status === 'warning',
                    'text-blue-500': result.status === 'info'
                }">
                    {{ result.result }}
                </view>
            </view>
        </view>
        
        <!-- 使用说明 -->
        <view class="mt-4 p-4 bg-yellow-50 rounded-lg">
            <view class="text-sm font-bold mb-2">使用说明:</view>
            <view class="text-xs text-gray-600">
                <view>1. 通过 pages/login/login?qudaoid=0773020 进入测试</view>
                <view>2. 点击测试按钮验证各项功能</view>
                <view>3. 查看测试结果确认功能正常</view>
            </view>
        </view>
    </view>
</template>

<style scoped></style>
